package nix.web.action;

import nix.domain.LinkMan;
import nix.service.LinkManService;
import nix.utils.PageBean;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class LinkManAction extends ActionSupport implements ModelDriven<LinkMan> {

	private LinkManService linkManService;
	private LinkMan linkMan = new LinkMan();
	private Integer currentPage;
	private Integer pageSize;

	public String add() throws Exception {
		
		linkManService.save(linkMan);		
		return "toList";
	}
	
	public String list() throws Exception {

		DetachedCriteria dc = DetachedCriteria.forClass(LinkMan.class);
		if (StringUtils.isNoneBlank(this.linkMan.getLkm_name())) {
			dc.add(Restrictions.like("lkm_name", "%" + this.linkMan.getLkm_name() + "%"));
		}
		if (this.linkMan.getCustomer() != null && this.linkMan.getCustomer().getCust_id() != null) {
			dc.add(Restrictions.eq("customer.cust_id", this.linkMan.getCustomer().getCust_id()));
		}
		PageBean pb = linkManService.getPageBean(dc, currentPage, pageSize);
		ActionContext.getContext().put("pageBean", pb);
		return "list";
	}
	
	public String toEdit() throws Exception {

		LinkMan lm = linkManService.getById(linkMan.getLkm_id());
		ActionContext.getContext().put("linkMan", lm);
		return "edit";
	}

	@Override
	public LinkMan getModel() {
		return this.linkMan;
	}	
	
	public void setLinkManService(LinkManService linkManService) {
		this.linkManService = linkManService;
	}

	public Integer getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}
}
